package org.xs.web.http.validate;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ValidateMap {

	private String fileName;
	private File file;
	private long reloadTime = 0l;
	
	public List<ValidateEntry> entrys = null;
	
	private Map<String,ValidateEntry> locale = new HashMap<String, ValidateEntry>();
	private ValidateRule validateRule;
	
	public ValidateMap(String fileName,File file,ValidateRule validateRule,List<ValidateEntry> entrys){
		this.fileName = fileName;
		this.file = file;
		this.validateRule = validateRule; 
		this.reloadTime = file.lastModified();
		this.entrys = entrys;
		init();
	}
	
	
	public ValidateEntry getValidates(String ctr,String action){
		if(file.lastModified() > reloadTime){
			entrys = validateRule.loadValidate(file);
			init();
			reloadTime = file.lastModified();
		}
		ValidateEntry res = ctr.length() < 1? locale.get(action) : locale.get(ctr+"_"+action);
		return res;
	}


	private void init() {
		locale.clear();
		for(ValidateEntry e : entrys){
			if(e.getController() == null || e.getController().length() < 1){
				locale.put(e.getAction(), e);
			}else{
				locale.put(e.getController()+"_"+e.getAction(), e);
			}
		}
	}
	
}
